<?php
namespace app\admin\controller;

use think\Controller;
use app\admin\model\Paper;
use think\Db;
use app\admin\model\Research;
use app\admin\model\Term;
use app\admin\model\Winning;
use app\admin\model\Classes;
use app\admin\model\Class_hour;
use app\admin\model\Course;
use app\admin\model\User;
use app\admin\model\Training;
use app\admin\model\Train;
use think\Request;

class Index extends Controller
{
	public function _initialize()
	{
		if (! session('userInfo.status')) {
			return $this->error('请登录', url('admin/Login/login'));
		}
	}
	public function index()
	{
		$q = (session('userInfo.id'));
		$t = Term::select();
		$winning = Db::view('winning','id,user_id,term_id,winning,rating,winning_time,remark,state,create_time')
			->view('term','term','winning.term_id=term.id')
			->view('user','username','winning.user_id=user.id')
			->where('user_id',$q)
			->order('id','desc')
			->paginate(3);

		$Page = $winning->render();
		$this->assign('page', $Page);
		$this->assign('tList', $t);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('winningList',$winning);

		return $this->fetch();
	}
	public function index2()
	{
		$q = (session('userInfo.id'));
		$t = Term::select();
		$c = Classes::select();
		$training = Db::view('training','id,user_id,term_id,class_id,name,weeks,training_nub,state,create_time')
			->view('classes','class_name,class_nub','training.class_id=classes.id')
			->view('term','term','training.term_id=term.id')
			->view('user','username','training.user_id=user.id')
			->where('user_id',$q)
			->order('id','desc')
			->paginate(3);
		$Page = $training->render();
		$this->assign('page', $Page);
		$this->assign('tList', $t);
		$this->assign('cList', $c);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('trainingList',$training);
		return $this->fetch();
	}
	public function index3()
	{
		$q = (session('userInfo.id'));
		$t = Term::select();
		$c = Classes::select();
		$cu = Course::select();
		$ch = Db::view('class_hour','id,user_id,term_id,class_id,course_id,plan_hour,hour,state,create_time')
			->view('classes','class_name,class_nub','class_hour.class_id=classes.id')
			->view('term','term','class_hour.term_id=term.id')
			->view('course','course_name','class_hour.course_id=course.id')
			->view('user','username','class_hour.user_id=user.id')
			->where('user_id',$q)
			->order('id','desc')
			->paginate(3);

		$Page = $ch->render();
		$this->assign('page', $Page); 
		$this->assign('tList', $t);
		$this->assign('cList',$c);
		$this->assign('cuList',$cu);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('chList',$ch);
		return $this->fetch();
	}
	public function index4()
	{
		$q = (session('userInfo.id'));
		$t = Term::select();
		$research = Db::view('research','id,user_id,term_id,entry_name,project_time,closing_time,level,state,create_time')
			->view('term','term','research.term_id=term.id')
			->view('user','username','research.user_id=user.id')
			->where('user_id',$q)
			->order('id','desc')
			->paginate(3);

		$Page = $research->render();
		$this->assign('page', $Page);
		$this->assign('tList', $t);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('researchList',$research);
		return $this->fetch();
	}
	public function index5()
	{
		$q = (session('userInfo.id'));
		$t = Term::select();
		$paper = Db::view('paper','id,user_id,term_id,title,journal_name,publish,ranking_order,grade,state,create_time')
			->view('term','term','paper.term_id=term.id')
			->view('user','username','paper.user_id=user.id')
			->where('user_id',$q)
			->order('id','desc')
			->paginate(3);

		$Page = $paper->render();
		$this->assign('page', $Page);
		$this->assign('tList', $t);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('paperList',$paper);
		return $this->fetch();
	}
	public function index6()
	{
		$q = (session('userInfo.id'));
		$t = Term::select();
		$train = Db::view('train','id,user_id,term_id,train_time,train_program,train_location,train_days,state,create_time')
			->view('term','term','train.term_id=term.id')
			->view('user','username','train.user_id=user.id')
			->where('user_id',$q)
			->order('id','desc')
			->paginate(3);

		$Page = $train->render();
		$this->assign('page', $Page);
		$this->assign('tList', $t);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('trainList',$train);
		return $this->fetch();
	}
	public function index7()
	{
		$t = Term::select();
		$q = (session('userInfo.id'));
		$w_data= Db::name('winning')->where('user_id',$q)->count(); //统计获奖的总共记录
		$t_data= Db::name('training')->where('user_id',$q)->sum('weeks');//统计实训的所有周数和
		$h_data= Db::name('class_hour')->where('user_id',$q)->sum('plan_hour');//统计授课的课时和
		$r_data= Db::name('research')->where('user_id',$q)->count();//统计科研项目数
		$p_data= Db::name('paper')->where('user_id',$q)->count();//统计论文发表数
		$tr_data= Db::name('train')->where('user_id',$q)->sum('train_days');//统计培训(出差)天数
		session("w_data",$w_data);
		session("t_data",$t_data);
		session("h_data",$h_data);
		session("r_data",$r_data);
		session("p_data",$p_data);
		session("tr_data",$tr_data);
		$this->assign('tList', $t);
		return $this->fetch();
	}
	// 获奖统计功能处理
	public function w_add(Request $request)
	{
		$u = $request->param("user_id");
		$t = $request->param("term_id");
		$s = $request->param("state");
		$wi = $request->param("winning");
		$ra = $request->param("rating");
		$time=$request->param("winning_time");
		if(empty($wi)){
			$this->error('获奖项目不能为空！');
		}
		if(empty($ra)){
			$this->error('奖励等级不能为空！');
		}
		if(empty($time)){
			$this->error('获奖时间不能为空！');
		}
		$intt=strtotime($time);
		$data=array(
			'user_id'=>$u,
			'term_id'=>$t,
			'state'=>$s,
			'winning'=>$wi,
			'rating'=>$ra,
			'winning_time'=>$intt
		);
		$res = Winning::create($data);
		if ($res) {
			$this->redirect('Index/index',302);
		} else {
			$this->error('添加失败');
		}
	}
	public function w_del(Request $request)
	{
		$id = $request->param('id');
		$res = Winning::destroy($id);
		if ($res) {
			$this->redirect('Index/index',302);
		} else {
			$this->error('删除失败');
		}
	}
	public function w_find($id){
		$q = (session('userInfo.id'));
		$winning = Db::view('winning','id,user_id,term_id,winning,rating,winning_time,remark,state,create_time')
			->view('term','term','winning.term_id=term.id')
			->view('user','username','winning.user_id=user.id')
			->where('user_id',$q)
			->where('id',$id)
			->find();
			return json($winning);
	}
	public function w_term(){
		$id=$_GET['term_id'];
		$q = (session('userInfo.id'));
		$winning = Db::view('winning','id,user_id,term_id,winning,rating,winning_time,remark,state,create_time')
			->view('term','term','winning.term_id=term.id')
			->view('user','username','winning.user_id=user.id')
			->where('user_id',$q)
			->where('term_id',$id)
			->find();
		session('term_id',$id);
		return json($winning);
	}
	public function wzt(Request $request)
	{
		$id = $request->param('id');
		$model = Winning::get($id);
		$model->state=1;
		$res = $model->save();
		if ($res) {
			$this->redirect('Index/index',302);
		} else {
			$this->error('提交失败');
		}
	}
	public function w_save(Request $request){
		$id = $request->param("id");
		$u = $request->param("user_id");
		$t = $request->param("term_id");
		$s = $request->param("state");
		$wi = $request->param("winning");
		$ra = $request->param("rating");
		$time=$request->param("winning_time");
		if(empty($wi)){
			$this->error('获奖项目不能为空！');
		}
		if(empty($ra)){
			$this->error('奖励等级不能为空！');
		}
		if(empty($time)){
			$this->error('获奖时间不能为空！');
		}
		$intt=strtotime($time);
		$res=Winning::update([
			'id'=>$id,
			'user_id'=>$u,
			'term_id'=>$t,
			'state'=>$s,
			'winning'=>$wi,
			'rating'=>$ra,
			'winning_time'=>$intt
			]);
		if(!$res==0){
			$this->redirect('Index/index',302);
		}else{
			$this->error('数据未更新！');
		}
	}
	public function w_search(Request $request){
		$term_id=$request->param("term_id");
		if($term_id=="选择学期"){
			$wp = $request->param('search');
			$q = (session('userInfo.id'));
			$t = Term::select();
			$winning = Db::view('winning','id,user_id,term_id,winning,rating,winning_time,remark,state,create_time')
				->view('term','term','winning.term_id=term.id')
				->view('user','username','winning.user_id=user.id')
				->where('user_id',$q)
				->where('term|winning|rating|winning_time|username','like','%'.$wp.'%')
				->order('id','desc')
				->paginate(3,false,['query'=>request()->param()]);
			$Page = $winning->render();
			$this->assign('page', $Page);
			$this->assign('tList', $t);
			$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
			$this->assign('winningList',$winning);
			return $this->fetch('index');
		}else{
			$wp = $request->param('search');
			$q = (session('userInfo.id'));
			$t = Term::select();
			$winning = Db::view('winning','id,user_id,term_id,winning,rating,winning_time,remark,state,create_time')
				->view('term','term','winning.term_id=term.id')
				->view('user','username','winning.user_id=user.id')
				->where('user_id',$q)
				->where('term|winning|rating|winning_time|username','like','%'.$wp.'%')
				->where('term_id',$term_id)
				->order('id','desc')
				->paginate(3,false,['query'=>request()->param()]);
			$Page = $winning->render();
			$this->assign('page', $Page);
			$this->assign('tList', $t);
			$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
			$this->assign('winningList',$winning);
			return $this->fetch('index');
		}
	}
	// 实训统计功能处理
	public function s_add(Request $request)
	{
		$data = $request->param();
		if(empty($data["name"])){
			$this->error('实训名称不能为空！');
		}
		if(empty($data["weeks"])){
			$this->error('实训周数不能为空！');
		}
		$res = Training::create($data);
		if ($res) {
			$this->redirect('Index/index2',302);
		} else {
			$this->error('添加失败');
		}
	}
	public function s_del(Request $request)
	{
		$id = $request->param('id');
		$res = Training::destroy($id);
		if ($res) {
			$this->redirect('Index/index2',302);
		} else {
			$this->error('删除失败');
		}
	}
	public function s_find($id){
		$q = (session('userInfo.id'));
		$training = Db::view('training','id,user_id,term_id,class_id,name,weeks,training_nub,state,create_time')
			->view('classes','class_name,class_nub','training.class_id=classes.id')
			->view('term','term','training.term_id=term.id')
			->view('user','username','training.user_id=user.id')
			->where('user_id',$q)
			->where('id',$id)
			->find();
			return json($training);
	}
	public function szt(Request $request)
	{
		$id = $request->param('id');
		$model = Training::get($id);
		$model->state=1;
		$res = $model->save();
		if ($res) {
			$this->redirect('Index/index2',302);
		} else {
			$this->error('提交失败');
		}
	}
	public function s_save(Request $request){
		$data=$request->param();
		if(empty($data['training_nub'])){
			$this->error('实训人数不能为空！');
		}
		if($data['training_nub']==0){
			$this->error('实训人数不能为零!');
		}
		if(empty($data['name'])){
			$this->error('实训名称不能为空！');
		}
		if(empty($data['weeks'])){
			$this->error('实训周数不能为空！');
		}
		$res=training::update($data);
		if(!$res==0){
			$this->redirect('Index/index2',302);
		}else{
			$this->error('数据未更新！');
		}
	}
	public function s_search(Request $request){
		$term_id=$request->param("term_id");
		if($term_id=="选择学期"){
			$wp = Request::instance()->get('search');
			$q = (session('userInfo.id'));
			$t = Term::select();
			$c = Classes::select();
			$training = Db::view('training','id,user_id,term_id,class_id,name,weeks,training_nub,state,create_time')
				->view('classes','class_name,class_nub','training.class_id=classes.id')
				->view('term','term','training.term_id=term.id')
				->view('user','username','training.user_id=user.id')
				->where('term|class_name|training_nub|name|weeks|username','like','%'.$wp.'%')
				->order('id','desc')
				->paginate(3,false,['query'=>request()->param()]);
			$Page = $training->render();
			$this->assign('page', $Page);
			$this->assign('tList', $t);
			$this->assign('cList', $c);
			$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
			$this->assign('trainingList',$training);
			return $this->fetch('index2');
		}	
		$wp = Request::instance()->get('search');
		$q = (session('userInfo.id'));
		$t = Term::select();
		$c = Classes::select();
		$training = Db::view('training','id,user_id,term_id,class_id,name,weeks,training_nub,state,create_time')
			->view('classes','class_name,class_nub','training.class_id=classes.id')
			->view('term','term','training.term_id=term.id')
			->view('user','username','training.user_id=user.id')
			->where('term|class_name|training_nub|name|weeks|username','like','%'.$wp.'%')
			->where('term_id',$term_id)
			->order('id','desc')
			->paginate(3,false,['query'=>request()->param()]);
		$Page = $training->render();
		$this->assign('page', $Page);
		$this->assign('tList', $t);
		$this->assign('cList', $c);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('trainingList',$training);
		return $this->fetch('index2');
	}
	// 授课统计功能处理
	public function k_add(Request $request)
	{
		$data = $request->param();
		if(empty($data["plan_hour"])){
			$this->error('计划课时不能为空！');
		}
		if(empty($data["hour"])){
			$this->error('实际课时不能为空！');
		}
		$res = Class_hour::create($data);
		if ($res) {
			$this->redirect('Index/index3',302);
		} else {
			$this->error('添加失败');
		}
	}
	public function k_del(Request $request)
	{
		$id = $request->param('id');
		$res=Class_hour::destroy($id);
		if ($res) {
			$this->redirect('Index/index3',302);
		} else {
			$this->error('删除失败');
		}
	}
	public function k_find($id){
		$q = (session('userInfo.id'));
		$ch = Db::view('class_hour','id,user_id,term_id,class_id,course_id,plan_hour,hour,state,create_time')
			->view('classes','class_name,class_nub','class_hour.class_id=classes.id')
			->view('term','term','class_hour.term_id=term.id')
			->view('course','course_name','class_hour.course_id=course.id')
			->view('user','username','class_hour.user_id=user.id')
			->where('user_id',$q)
			->where('id',$id)
			->find();
			return json($ch);
	}
	public function k_zt(Request $request)
	{
		$id = $request->param('id');
		$model = Class_hour::get($id);
		$model->state=1;
		$res = $model->save();
		if ($res) {
			$this->redirect('Index/index3',302);
		} else {
			$this->error('提交失败');
		}
	}
	public function k_save(Request $request){
		$data=$request->param();
		if(empty($data['plan_hour'])){
			$this->error('计划课时不能为空！');
		}
		if($data['hour']==0){
			$this->error('实际课时不能为零!');
		}
		$res=Class_hour::update($data);
		if(!$res==0){
			$this->redirect('Index/index3',302);
		}else{
			$this->error('数据未更新！');
		}
	}
	public function k_search(Request $request){	
		$term_id=$request->param("term_id");
		if($term_id=="选择学期"){
			$wp = Request::instance()->get('search');
		$q = (session('userInfo.id'));
		$t = Term::select();
		$c = Classes::select();
		$cu = Course::select();
		$ch = Db::view('class_hour','id,user_id,term_id,class_id,course_id,plan_hour,hour,state,create_time')
			->view('classes','class_name,class_nub','class_hour.class_id=classes.id')
			->view('term','term','class_hour.term_id=term.id')
			->view('course','course_name','class_hour.course_id=course.id')
			->view('user','username','class_hour.user_id=user.id')
			->where('user_id',$q)
			->where('term|class_name|class_nub|course_name|plan_hour|hour|username','like','%'.$wp.'%')
			->order('id','desc')
			->paginate(3,false,['query'=>request()->param()]);
		$Page = $ch->render();
		$this->assign('page', $Page); 
		$this->assign('tList', $t);
		$this->assign('cList',$c);
		$this->assign('cuList',$cu);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('chList',$ch);
		return $this->fetch('index3');
		}
		$wp = Request::instance()->get('search');
		$q = (session('userInfo.id'));
		$t = Term::select();
		$c = Classes::select();
		$cu = Course::select();
		$ch = Db::view('class_hour','id,user_id,term_id,class_id,course_id,plan_hour,hour,state,create_time')
			->view('classes','class_name,class_nub','class_hour.class_id=classes.id')
			->view('term','term','class_hour.term_id=term.id')
			->view('course','course_name','class_hour.course_id=course.id')
			->view('user','username','class_hour.user_id=user.id')
			->where('user_id',$q)
			->where('term_id',$term_id)
			->where('term|class_name|class_nub|course_name|plan_hour|hour|username','like','%'.$wp.'%')
			->order('id','desc')
			->paginate(3,false,['query'=>request()->param()]);
		$Page = $ch->render();
		$this->assign('page', $Page); 
		$this->assign('tList', $t);
		$this->assign('cList',$c);
		$this->assign('cuList',$cu);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('chList',$ch);
		return $this->fetch('index3');		
	}
	// 科研项目统计功能处理
	public function ky_add(Request $request)
	{
		$data = $request->param();
		if(empty($data["entry_name"])){
			$this->error('科研项目不能为空！');
		}
		if(empty($data["level"])){
			$this->error('项目级别不能为空！');
		}
		if(empty($data["project_time"])){
			$this->error('立项时间不能为空！');
		}
		if(empty($data["closing_time"])){
			$this->error('结题时间不能为空！');
		}
		$res = Research::create($data);
		if ($res) {
			$this->redirect('Index/index4',302);
		} else {
			$this->error('添加失败');
		}
	}
	public function ky_del(Request $request)
	{
		$id = $request->param('id');
		$res=Research::destroy($id);
		if ($res) {
			$this->redirect('Index/index4',302);
		} else {
			$this->error('删除失败');
		}
	}
	public function ky_find($id){
		$q = (session('userInfo.id'));
		$research = Db::view('research','id,user_id,term_id,entry_name,project_time,closing_time,level,state,create_time')
			->view('term','term','research.term_id=term.id')
			->view('user','username','research.user_id=user.id')
			->where('user_id',$q)
			->where('id',$id)
			->find();
			return json($research);
	}
	public function ky_zt(Request $request)
	{
		$id = $request->param('id');
		$model = Research::get($id);
		$model->state=1;
		$res = $model->save();
		if ($res) {
			$this->redirect('Index/index4',302);
		} else {
			$this->error('提交失败');
		}
	}
	public function ky_save(Request $request){
		$id = $request->param("id");
		$u = $request->param("user_id");
		$t = $request->param("term_id");
		$s = $request->param("state");
		$en = $request->param("entry_name");
		$le = $request->param("level");
		$ptime=$request->param("project_time");
		$ctime=$request->param("closing_time");
		if(empty($en)){
			$this->error('科研项目不能为空！');
		}
		if(empty($le)){
			$this->error('科研级别不能为空！');
		}
		if(empty($ptime)){
			$this->error('立项时间不能为空！');
		}
		if(empty($ctime)){
			$this->error('结题时间不能为空！');
		}
		$intpt=strtotime($ptime);
		$intct=strtotime($ctime);
		$res=Research::update([
			'id'=>$id,
			'user_id'=>$u,
			'term_id'=>$t,
			'state'=>$s,
			'entry_name'=>$en,
			'level'=>$le,
			'project_time'=>$intpt,
			'closing_time'=>$intct
			]);
		if(!$res==0){
			$this->redirect('Index/index4',302);
		}else{
			$this->error('数据未更新！');
		}
	}
	public function ky_search(Request $request){
		$term_id=$request->param("term_id");
		if($term_id=="选择学期"){
			$wp = Request::instance()->get('search');
			$q = (session('userInfo.id'));
			$t = Term::select();
			$research = Db::view('research','id,user_id,term_id,entry_name,project_time,closing_time,level,state,create_time')
				->view('term','term','research.term_id=term.id')
				->view('user','username','research.user_id=user.id')
				->where('user_id',$q)
				->where('term|entry_name|project_time|closing_time|level|username','like','%'.$wp.'%')
				->order('id','desc')
				->paginate(3,false,['query'=>request()->param()]);
			$Page = $research->render();
			$this->assign('page', $Page);
			$this->assign('tList', $t);
			$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
			$this->assign('researchList',$research);
			return $this->fetch('index4');
		}		
		$wp = Request::instance()->get('search');
		$q = (session('userInfo.id'));
		$t = Term::select();
		$research = Db::view('research','id,user_id,term_id,entry_name,project_time,closing_time,level,state,create_time')
			->view('term','term','research.term_id=term.id')
			->view('user','username','research.user_id=user.id')
			->where('user_id',$q)
			->where('term_id',$term_id)
			->where('term|entry_name|project_time|closing_time|level|username','like','%'.$wp.'%')
			->order('id','desc')
			->paginate(3,false,['query'=>request()->param()]);
		$Page = $research->render();
		$this->assign('page', $Page);
		$this->assign('tList', $t);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('researchList',$research);
		return $this->fetch('index4');
	}
	// 论文统计功能处理
	public function lunw_add(Request $request)
	{
		$data = $request->param();
		if(empty($data["title"])){
			$this->error('论文题目不能为空！');
		}
		if(empty($data["journal_name"])){
			$this->error('刊物名称不能为空！');
		}
		if(empty($data["publish"])){
			$this->error('发表期号不能为空！');
		}
		if(empty($data["ranking_order"])){
			$this->error('排名顺序不能为空！');
		}
		if(empty($data["grade"])){
			$this->error('等级不能为空！');
		}
		$res = Paper::create($data);
		if ($res) {
			$this->redirect('Index/index5',302);
		} else {
			$this->error('添加失败');
		}
	}
	public function lunw_del(Request $request)
	{
		$id = $request->param('id');
		$res=Paper::destroy($id);
		if ($res) {
			$this->redirect('Index/index5',302);
		} else {
			$this->error('删除失败');
		}
	}
	public function lunw_find($id){
		$q = (session('userInfo.id'));
		$paper = Db::view('paper','id,user_id,term_id,title,journal_name,publish,ranking_order,grade,state,create_time')
			->view('term','term','paper.term_id=term.id')
			->view('user','username','paper.user_id=user.id')
			->where('user_id',$q)
			->where('id',$id)
			->find();
			return json($paper);
	}
	public function lunw_zt(Request $request)
	{
		$id = $request->param('id');
		$model = Paper::get($id);
		$model->state=1;
		$res = $model->save();
		if ($res) {
			$this->redirect('Index/index5',302);
		} else {
			$this->error('提交失败');
		}
	}
	public function lunw_save(Request $request){
		$id = $request->param("id");
		$u = $request->param("user_id");
		$t = $request->param("term_id");
		$s = $request->param("state");
		$ti = $request->param("title");
		$jn = $request->param("journal_name");
		$pu =$request->param("publish");
		$ro = $request->param("ranking_order");
		$g = $request->param("grade");
		if(empty($ti)){
			$this->error('论文题目不能为空！');
		}
		if(empty($jn)){
			$this->error('刊物名称不能为空！');
		}
		if(empty($pu)){
			$this->error('发表期号不能为空！');
		}
		if(empty($ro)){
			$this->error('排名顺序不能为空！');
		}
		if(empty($g)){
			$this->error('论文等级不能为空！');
		}
		$intt=strtotime($pu);
		$res=Paper::update([
			'id'=>$id,
			'user_id'=>$u,
			'term_id'=>$t,
			'state'=>$s,
			'title'=>$ti,
			'journal_name'=>$jn,
			'publish'=>$intt,
			'ranking_order'=>$ro,
			'grade'=>$g
			]);
		if(!$res==0){
			$this->redirect('Index/index5',302);
		}else{
			$this->error('数据未更新！');
		}
	}
	public function lunw_search(Request $request){
		$term_id=$request->param("term_id");
		if($term_id=="选择学期"){
			$wp = Request::instance()->get('search');
			$q = (session('userInfo.id'));
			$t = Term::select();
			$paper = Db::view('paper','id,user_id,term_id,title,journal_name,publish,ranking_order,grade,state,create_time')
				->view('term','term','paper.term_id=term.id')
				->view('user','username','paper.user_id=user.id')
				->where('user_id',$q)
				->where('term|journal_name|title|publish|ranking_order|grade|username','like','%'.$wp.'%')
				->order('id','desc')
				->paginate(3,false,['query'=>request()->param()]);
			$Page = $paper->render();
			$this->assign('page', $Page);
			$this->assign('tList', $t);
			$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
			$this->assign('paperList',$paper);
			return $this->fetch('index5');
		}	
		$wp = Request::instance()->get('search');
		$q = (session('userInfo.id'));
		$t = Term::select();
		$paper = Db::view('paper','id,user_id,term_id,title,journal_name,publish,ranking_order,grade,state,create_time')
			->view('term','term','paper.term_id=term.id')
			->view('user','username','paper.user_id=user.id')
			->where('user_id',$q)
			->where('term_id',$term_id)
			->where('term|journal_name|title|publish|ranking_order|grade|username','like','%'.$wp.'%')
			->order('id','desc')
			->paginate(3,false,['query'=>request()->param()]);
		$Page = $paper->render();
		$this->assign('page', $Page);
		$this->assign('tList', $t);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('paperList',$paper);
		return $this->fetch('index5');
	}
	// 培训（出差）统计功能处理
	public function peix_add(Request $request)
	{
		$data = $request->param();
		if(empty($data["train_time"])){
			$this->error('培训(出差)时间不能为空！');
		}
		if(empty($data["train_program"])){
			$this->error('培训项目(出差原因)不能为空！');
		}
		if(empty($data["train_location"])){
			$this->error('培训(出差)地点不能为空！');
		}
		if(empty($data["train_days"])){
			$this->error('培训(出差)天数不能为空！');
		}
		$res = Train::create($data);
		if ($res) {
			$this->redirect('Index/index6',302);
		} else {
			$this->error('添加失败');
		}
	}
	public function peix_del(Request $request)
	{
		$id = $request->param('id');
		$res=Train::destroy($id);
		if ($res) {
			$this->redirect('Index/index6',302);
		} else {
			$this->error('删除失败');
		}
	}
	public function peix_find($id){
		$q = (session('userInfo.id'));
		$train = Db::view('train','id,user_id,term_id,train_time,train_program,train_location,train_days,state,create_time')
			->view('term','term','train.term_id=term.id')
			->view('user','username','train.user_id=user.id')
			->where('user_id',$q)
			->where('id',$id)
			->find();
			return json($train);
	}
	public function peix_zt(Request $request)
	{
		$id = $request->param('id');
		$model = Train::get($id);
		$model->state=1;
		$res = $model->save();
		if ($res) {
			$this->redirect('Index/index6',302);
		} else {
			$this->error('提交失败');
		}
	}
	public function peix_save(Request $request){
		$id = $request->param("id");
		$u = $request->param("user_id");
		$t = $request->param("term_id");
		$s = $request->param("state");
		$time = $request->param("train_time");
		$tp = $request->param("train_program");
		$tl =$request->param("train_location");
		$td = $request->param("train_days");
		if(empty($time)){
			$this->error('培训(出差)时间不能为空！');
		}
		if(empty($tp)){
			$this->error('培训项目(出差原因)不能为空！');
		}
		if(empty($tl)){
			$this->error('培训(出差)地点不能为空！');
		}
		if(empty($td)){
			$this->error('培训(出差)天数不能为空！');
		}
		$intt=strtotime($time);
		$res=Train::update([
			'id'=>$id,
			'user_id'=>$u,
			'term_id'=>$t,
			'state'=>$s,
			'train_time'=>$intt,
			'train_program'=>$tp,
			'train_location'=>$tl,
			'train_days'=>$td
			]);
		if(!$res==0){
			$this->redirect('Index/index6',302);
		}else{
			$this->error('数据未更新！');
		}
	}
	public function peix_search(Request $request){
		$term_id=$request->param("term_id");
		if($term_id=="选择学期"){
			$wp = $request->param('search');
			$q = (session('userInfo.id'));
			$t = Term::select();
			$train = Db::view('train','id,user_id,term_id,train_time,train_program,train_location,train_days,state,create_time')
				->view('term','term','train.term_id=term.id')
				->view('user','username','train.user_id=user.id')
				->where('user_id',$q)
				->where('term|train_time|train_program|train_location|train_days|username','like','%'.$wp.'%')
				->order('id','desc')
				->paginate(3,false,['query'=>request()->param()]);
			$Page = $train->render();
			$this->assign('page', $Page);
			$this->assign('tList', $t);
			$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
			$this->assign('trainList',$train);
			return $this->fetch('index6');
		}	
		$wp = $request->param('search');
		$q = (session('userInfo.id'));
		$t = Term::select();
		$train = Db::view('train','id,user_id,term_id,train_time,train_program,train_location,train_days,state,create_time')
			->view('term','term','train.term_id=term.id')
			->view('user','username','train.user_id=user.id')
			->where('user_id',$q)
			->where('term_id',$term_id)
			->where('term|train_time|train_program|train_location|train_days|username','like','%'.$wp.'%')
			->order('id','desc')
			->paginate(3,false,['query'=>request()->param()]);
		$Page = $train->render();
		$this->assign('page', $Page);
		$this->assign('tList', $t);
		$this->assign('empty','<span class="empty" style="color:red;position:absolute;top:32%;left:52%;">没有数据</span>');
		$this->assign('trainList',$train);
		return $this->fetch('index6');
	}
	// 密码校验
	public function user_password(Request $request)
	{
		$userid=$request->param('user_id');
		$password = $request->param('password');
		$jmm = md5($password);
		$newpassword = $request->param('newpassword');
		$rpassword = $request->param('rpassword');
		$jm = md5($rpassword);
		if(empty($password)){
			$this->error('原密码不能为空！');
		}
		if(empty($newpassword)){
			$this->error('新密码不能为空！');
		}
		if(empty($rpassword)){
			$this->error('确认密码不能为空！');
		}
		if($newpassword!=$rpassword){
			$this->error('新密码和二次确认密码不正确');
		}
		$data01=array(
			'passwd' => $jmm
		);
		$res = User::get($data01);
		if (!$res) {
			$this->error("原始密码不正确");
		}
		$user = new User;
		$list = [
		    ['id'=>$userid, 'passwd'=>$jm]
		];
		$ress=$user->isUpdate()->saveAll($list);
		if(!$ress){
			$this->error("修改密码错误");
		}else{
			session('userInfo',null);
			$this->success("修改成功，请重新登录!", url('admin/Login/login'),1);
		}
	}

	public function user_class(Request $request){
		$term_id=$request->param();
		$t = Term::select();
		$q = (session('userInfo.id'));
		$this->assign('tList', $t);
		if($term_id=="选择学期"){
			$w_data= Db::name('winning')->where('user_id',$q)->count(); //统计获奖的总共记录
			$t_data= Db::name('training')->where('user_id',$q)->sum('weeks');//统计实训的所有周数和
			$h_data= Db::name('class_hour')->where('user_id',$q)->sum('plan_hour');//统计授课的课时和
			$r_data= Db::name('research')->where('user_id',$q)->count();//统计科研项目数
			$p_data= Db::name('paper')->where('user_id',$q)->count();//统计论文发表数
			$tr_data= Db::name('train')->where('user_id',$q)->sum('train_days');//统计培训(出差)天数
			session("w_data",$w_data);
			session("t_data",$t_data);
			session("h_data",$h_data);
			session("r_data",$r_data);
			session("p_data",$p_data);
			session("tr_data",$tr_data);
			return $this->fetch("index7");
		}else{
			$w_data= Db::name('winning')->where('user_id',$q)->count(); //统计获奖的总共记录
			$t_data= Db::name('training')->where('user_id',$q)->sum('weeks');//统计实训的所有周数和
			$h_data= Db::name('class_hour')->where('user_id',$q)->sum('plan_hour');//统计授课的课时和
			$r_data= Db::name('research')->where('user_id',$q)->count();//统计科研项目数
			$p_data= Db::name('paper')->where('user_id',$q)->count();//统计论文发表数
			$tr_data= Db::name('train')->where('user_id',$q)->sum('train_days');//统计培训(出差)天数
			session("w_data",$w_data);
			session("t_data",$t_data);
			session("h_data",$h_data);
			session("r_data",$r_data);
			session("p_data",$p_data);
			session("tr_data",$tr_data);
			return $this->fetch("index7");
		}
	}
	public function guanl($id)
	{
		$cc = Classes::where('id',$id)->find();
		return json($cc);
	}
}